﻿using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using HiSportApplet.Application.Common.SqlSugar;
using HiSportApplet.Application.Service.Bas.BasSiteManagement.Dtos;
using HiSportApplet.Application.Service.Bas.BasVenuesManagement.Dtos;
using HiSportApplet.Core.Entity.Bas;
using HiSportApplet.Core.Entity.Sys;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis;
using SqlSugar;

namespace HiSportApplet.Application.Service.Bas.BasSiteManagement
{
    /// <summary>
    /// 场地管理
    /// </summary>
    [ApiDescriptionSettings("基础资料", Tag = "场地管理", SplitCamelCase = false)]
    public class BasSiteService : ITransient, IBasSiteService, IDynamicApiController
    {
        private readonly SqlSugarRepository<BasVenues> _basVenuesRep;    
        private readonly SqlSugarRepository<BasSite> _basSiteRep;
        private readonly SqlSugarRepository<BasSiteType> _basSiteTypeRep;
       // private readonly SqlSugarRepository<OrderSiteInfo> _orderSiteInfo;

        public BasSiteService(
            SqlSugarRepository<BasVenues> basVenuesRep, 
            SqlSugarRepository<BasSite> basSiteRep,
            SqlSugarRepository<BasSiteType> basSiteTypeRep

            )
        {
            _basVenuesRep = basVenuesRep;                  
            _basSiteRep = basSiteRep;
            _basSiteTypeRep = basSiteTypeRep;
        }



        /// <summary>
        /// 查询某个球场某天某个类型的场地时间段详细信息
        /// </summary>
        /// <returns></returns>
        public async Task<List<ScheduledSiteDto>> SearchSiteDetails(SearchSiteInputDto input)
        {
            ParamNull(input);

            //查询场地类型
            var siteType = await _basSiteTypeRep.AsQueryable()
                .Where(x => x.Id == input.SiteTypeId)
                .FirstAsync();
            if (siteType == null)
            {
                throw Oops.Oh($"没有查询到此场地类型");
            }

            //获取类型下的所有场地
            var siteList = await _basSiteRep.AsQueryable()
                .Where(x => x.SiteTypeId == input.SiteTypeId)
                .ToListAsync();
            var siteIds = siteList.Select(x => x.Id);


            ////我的预定订单
            //var orderSiteInfo = await _orderSiteInfo.AsQueryable()
            //    .Where(x => siteIds.Contains(x.SiteId) && x.ScheduledTime.Date == input.DateTime.Date)
            //    .ToListAsync();
            //var orderSiteId = orderSiteInfo.GroupBy(x => x.OrderSiteId).Select(x => x.Key).ToList();
            //var orderSite = await _orderSite.AsQueryable().Where(x => orderSiteId.Contains(x.Id)).ToListAsync();



           // return result;

            return null;

        }


        /// <summary>
        /// 获取看板时间列表
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="interval">间隔时间：分钟为准</param>
        /// <returns></returns>
        private List<GetTimeSpanDto> GetTimeSpan(DateTime startTime, DateTime endTime, int interval)
        {
            List<GetTimeSpanDto> times = new List<GetTimeSpanDto>();
            for (DateTime i = startTime; i <= endTime; i = i.AddMinutes(interval))
            {
                if (i >= endTime) break;
                var end = i.AddMinutes(interval);
                if (end >= endTime)
                {
                    end = endTime;
                };
                var curValue = new GetTimeSpanDto { DateTimeStart = i, DateTimeEnd = end };
                times.Add(curValue);
            }
            return times;

        }



        /// <summary>
        /// 参数为NULL验证
        /// </summary>
        /// <param name="input"></param>
        private void ParamNull(object input)
        {
            if (input == null) throw Oops.Oh("参数错误:参数为NULL");
        }

    }

}
